import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_framework/utils/logs.dart';
import 'package:flutter_framework/utils/screen_ui.dart';

class AnimatedSnow extends StatefulWidget {
  const AnimatedSnow({super.key});

  @override
  State<AnimatedSnow> createState() => _AnimatedSnowState();
}

class _AnimatedSnowState extends State<AnimatedSnow>
    with SingleTickerProviderStateMixin {
  final _snowflakList = List.generate(100, (index) => Snowflak());
  late final AnimationController _animationController =
      AnimationController(vsync: this, duration: const Duration(seconds: 1));

  @override
  void initState() {
    // TODO: implement initState
    _animationController.repeat();
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('雪人下雪'),
      ),
      body: Container(
        constraints: const BoxConstraints.expand(),
        decoration: const BoxDecoration(
            gradient: LinearGradient(
                colors: [Colors.white, Colors.blue],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                stops: [0.0, 1])),
        child: AnimatedBuilder(
          animation: _animationController,
          builder: (BuildContext context, Widget? child) {
            _snowflakList.forEach((flak) {
              flak.fall();
            });
            return CustomPaint(
              painter: MyPainter(_snowflakList),
              child: CustomPaint(
                painter: SnowManPainter(),
              ),
            );
          },
        ),
      ),
    );
  }
}

class SnowManPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = Colors.white // 画笔的颜色
      ..strokeWidth = 2.0; // 线的宽度
    // TODO: implement paint
    // logs(size);
    // logs(size.height);
    final Rect rect2 = Rect.fromPoints(
      Offset(size.width / 2 - 100, size.height - 200),
      Offset(size.width / 2 + 100, size.height + 100),
    );
    final circle = Offset(size.width / 2, size.height - 230);
    canvas.drawOval(rect2, paint);
    canvas.drawCircle(circle, 50, paint);

    // canvas.drawCircle( Offset(size.width/2, size.height), 100, _paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

class MyPainter extends CustomPainter {
  MyPainter(this.list);

  final List<Snowflak> list;

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = Colors.white // 画笔的颜色
      ..strokeWidth = 2.0; // 线的宽度
    // TODO: implement paint
    // logs(size);
    // logs(size.height);
    // final Rect rect2 = Rect.fromPoints(
    //     Offset(size.width/2-100,  size.height-200),
    //     Offset(size.width/2+100,  size.height+100),
    //  );
    //  final circle = Offset(size.width/2, size.height-230);
    //  canvas.drawOval(rect2, paint);
    //  canvas.drawCircle(circle,50, paint);
    list.forEach((flak) {
      canvas.drawCircle(Offset(flak.x, flak.y), flak.radius, paint);
    });

    // canvas.drawCircle( Offset(size.width/2, size.height), 100, _paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
}

class Snowflak {
  // 画一个雪花
  double radius = Random().nextDouble() * 2 + 2;
  double x = Random().nextDouble() * ScreenUI.width;
  double y = Random().nextDouble() * ScreenUI.height;
  double speed = Random().nextDouble() * 1 + 1;

  void fall() {
    y += speed;
    if (y > ScreenUI.height) {
      y = 0;
      // nextdouble 生成 0-1之间的小数
      radius = Random().nextDouble() * 2 + 2;
      x = Random().nextDouble() * ScreenUI.width;
      speed = Random().nextDouble() * 4 + 2;
    }
  }
}
